package com.zm.devmag.device.service.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zm.devmag.device.Vo.DeviceDetaiVo;

import com.zm.devmag.device.dao.DeviceDetailMapper;
import com.zm.devmag.device.dao.IOStockTempMapper;
import com.zm.devmag.device.dao.InOutStockMapper;
import com.zm.devmag.device.po.Category;
import com.zm.devmag.device.po.DeviceDetail;
import com.zm.devmag.device.po.IOStockTemp;
import com.zm.devmag.device.po.InOutStock;
import com.zm.devmag.device.service.IOSTempServer;
import com.zm.devmag.user.dao.UsersMapper;
import com.zm.devmag.user.po.Users;




@Service
public class IOSTempServerImpl implements IOSTempServer {
	@Autowired
	private InOutStockMapper iosMapper;
	@Autowired
	private IOStockTempMapper iostMapper;
	@Autowired
	private DeviceDetailMapper ddMapper;
	@Autowired
	private UsersMapper userMapper;
	
	public Map<String, Object> insertInOutStock(String para,String uacc,String coflag,String type){//type1入库2出库
		 Map<String, Object> reJson = new HashMap<String, Object>();
		 DeviceDetaiVo ddvo = ddMapper.selectDeviceByNo(para);
		 if(ddvo ==null){
			 reJson.put("ok","sck");
			 return reJson;
		 }
		 Map<String,Object> map = new HashMap<String,Object>();
			map.put("coflag", coflag);
			map.put("did", ddvo.getId());
		    if( iostMapper.getScanMax(map)>=1){
		    	reJson.put("ok","ysm");
				 return reJson;
			 }
			 Users us = userMapper.SelectUserByUacc(uacc);
			 if(!us.getUtype().equals("1")){//非管理员操作
				 DeviceDetail dd = ddMapper.selectByIdRturnDD(ddvo.getId());
				 if(us.getWid()!=dd.getWid()){
					 if(type.equals("1")){
						 if(ddvo.getStatus().equals("1")){
							 reJson.put("ok","qtcyrk");
							 return reJson;
						 }else if (ddvo.getStatus().equals("2")){
							 reJson.put("ok","qtcyck");
							 reJson.put("para",para);
							 return reJson;
						 }else if (ddvo.getStatus().equals("3")){
							 reJson.put("ok","qtcybf");
							 return reJson;
						 }
						 
					 }else if(type.equals("2")){
						 reJson.put("ok","qtc");
						 return reJson;
					 }
					 
				 }
			 }
		 if(type.equals("1")){
			 if(ddvo.getStatus().equals("1")){
				 reJson.put("ok","yck");
				 return reJson;
			 } 
		 }else if(type.equals("2")){
			 if(ddvo.getStatus().equals("2")){
				 reJson.put("ok","yck");
				 return reJson;
			 } 
		 }
		 if(ddvo.getStatus().equals("3")){
			 reJson.put("ok","ybf");
			 return reJson;
		 }
		
		 Long maxid = iostMapper.getMaxIdValue();
		 if(maxid == null)maxid=Long.parseLong("0"); 
		 IOStockTemp iost = new  IOStockTemp();
		 iost.setId(maxid+1);
		 iost.setCoflag(coflag);
		 if(type.equals("1")){
			 iost.setCotype("1");
		 }else if(type.equals("2")){
			 iost.setCotype("2");
		 }
		 iost.setDid(ddvo.getId());
		 if(us.getUtype().equals("1")){//管理员只改变出入库状态，不能改变所在仓库位置
			 DeviceDetail dd = ddMapper.selectByIdRturnDD(ddvo.getId());
			 iost.setWid(dd.getWid());
		 }else{
			 iost.setWid(us.getWid());
		 }
		 
		 iost.setUid(us.getUid());
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 iost.setIotime(sdf.format(new Date()));
		 if(iostMapper.insertOneIOStockTemp(iost)>0){
			 reJson.put("iost", Long.toString(maxid+1));
			 reJson.put("ddvo", ddvo);
			 reJson.put("ok", "ok");
		 }
		 return reJson;
	}
	//扫其他部门的码转入本部门
	public Map<String, Object> OtherInsertInOutStock(String para,String uacc,String coflag){
		Map<String, Object> reJson = new HashMap<String, Object>();
		DeviceDetaiVo ddvo = ddMapper.selectDeviceByNo(para);
		Users us = userMapper.SelectUserByUacc(uacc);
		Long maxid = iostMapper.getMaxIdValue();
		if(maxid == null)maxid=Long.parseLong("0"); 
		IOStockTemp iost = new  IOStockTemp();
		iost.setId(maxid+1);
		iost.setCoflag(coflag);
		iost.setCotype("1");
		iost.setDid(ddvo.getId());
		iost.setWid(us.getWid());
		iost.setUid(us.getUid());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		iost.setIotime(sdf.format(new Date()));
		if(iostMapper.insertOneIOStockTemp(iost)>0){
			reJson.put("iost", Long.toString(maxid+1));
			reJson.put("ddvo", ddvo);
			reJson.put("ok", "ok");
		}
		return reJson;
	}
	public Map<String, Object> submitInOutStock(HttpServletRequest request,String type){
		
		String coflag="";
		if(type.equals("1")){
			coflag=(String) request.getSession().getAttribute("coflag");
		 }else if(type.equals("2")){
			 coflag=(String) request.getSession().getAttribute("outcoflag");
		 }
		
		 Map<String, Object> reJson = new HashMap<String, Object>();
		 if(coflag==null || coflag==""){
			 reJson.put("ok", "fal");
			 return reJson;
		 }
		 List<InOutStock> inlist = new ArrayList<InOutStock>();
		 List<IOStockTemp> iostlist = iostMapper.selectByCoflag(coflag);
		 for(IOStockTemp iost: iostlist){
			 DeviceDetail dd =  ddMapper.selectByIdRturnDD(iost.getDid());
			 InOutStock ios = new InOutStock();//插操作记录
			 ios.setDid(iost.getDid());
			 if(type.equals("1")){
				 ios.setCorm(Long.parseLong("2"));
				 dd.setStatus("1");//ruku 
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 dd.setRemark("入库时间:"+sdf.format(new Date()));
			 }else if(type.equals("2")){
				 ios.setCorm(Long.parseLong("3"));
				 dd.setStatus("2");//ruku 
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 dd.setRemark("出库时间:"+sdf.format(new Date()));
			 } 
			 ios.setOldpos(dd.getWid());
			 ios.setPos(iost.getWid());
			 ios.setCotime(iost.getIotime());
			 ios.setCouser(iost.getUid());
			 inlist.add(ios);
			 //dd.setStatus("1");//ruku 
			 dd.setWid(iost.getWid());
			 ddMapper.updateByPrimaryKeySelective(dd);
		 }
		 if( iosMapper.insertInOutStockBatch(inlist)>0){
			 reJson.put("ok", "ok");
			 iostMapper.deleteIOSTByCoflag(coflag);
			 if(type.equals("1")){
					 request.getSession().setAttribute("coflag",null);
				 }else if(type.equals("2")){
					 request.getSession().setAttribute("outcoflag",null);
				 }
			 return reJson;
		 }else{
			 reJson.put("ok", "fal");
			 return reJson;
		 }
		
	}  
	public Map<String, Object> insertOutStock(String para,String uacc,String coflag){
		Map<String, Object> reJson = new HashMap<String, Object>();
		 DeviceDetaiVo ddvo = ddMapper.selectDeviceByNo(para);
		 if(ddvo ==null){
			 reJson.put("ok","sck");
			 return reJson;
		 }
		 if(ddvo.getStatus().equals("2")){
			 reJson.put("ok","yck");
			 return reJson;
		 }else if(ddvo.getStatus().equals("3")){
			 reJson.put("ok","ybf");
			 return reJson;
		 }
		 Users us = userMapper.SelectUserByUacc(uacc);
		 Long maxid = iostMapper.getMaxIdValue();
		 if(maxid == null)maxid=Long.parseLong("0"); 
		 IOStockTemp iost = new  IOStockTemp();
		 iost.setId(maxid+1);
		 iost.setCoflag(coflag);
		 iost.setCotype("2");
		 iost.setDid(ddvo.getId());
		 iost.setWid(us.getWid());
		 iost.setUid(us.getUid());
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 iost.setIotime(sdf.format(new Date()));
		 if(iostMapper.insertOneIOStockTemp(iost)>0){
			 reJson.put("iost", Long.toString(maxid+1));
			 reJson.put("ddvo", ddvo);
			 reJson.put("ok", "ok");
		 }
		 return reJson;
	}
	public int deleteIOSTByCoflag(String coflag){
		return iostMapper.deleteIOSTByCoflag(coflag);
	}
}
